package com.baijiayun.liveuibase.skin;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.baijiayun.liveuibase.utils.ThemeDataUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by yongjiaming on 2020/6/16
 * Describe:
 */
public class SkinInflateFactory implements LayoutInflater.Factory {

    private List<SkinItem> mSkinItems = new ArrayList<>();

    @Override
    public View onCreateView(String name, Context context, AttributeSet attrs) {
        List<SkinAttr> viewAttrs = new ArrayList<>();
        View view = createView(context, name, attrs);
        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            String attrValue = attrs.getAttributeValue(i);
            if (!AttrFactory.isSupportedAttr(attrName)) {
                continue;
            }

            if (attrValue.startsWith("?") || attrValue.startsWith("@drawable/")) {
                try {
                    int id = Integer.parseInt(attrValue.substring(1));
                    String entryName = context.getResources().getResourceEntryName(id);
                    String typeName = context.getResources().getResourceTypeName(id);
                    SkinAttr mSkinAttr = AttrFactory.get(attrName, id, entryName, typeName);
                    if (mSkinAttr != null) {
                        viewAttrs.add(mSkinAttr);
                    }
                    SkinItem skinItem = new SkinItem();
                    skinItem.view = view;
                    skinItem.attrs = viewAttrs;
                    if(ThemeDataUtil.getThemeConfig() == null){
                        mSkinItems.add(skinItem);
                    } else{
                        skinItem.apply(ThemeDataUtil.getThemeConfig());
                    }
                } catch (NumberFormatException | Resources.NotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
        return view;
    }


    /**
     * Invoke low-level function for instantiating a view by name. This attempts to
     * instantiate a view class of the given <var>name</var> found in this
     * LayoutInflater's ClassLoader.
     *
     * @param context
     * @param name The full name of the class to be instantiated.
     * @param attrs The XML attributes supplied for this instance.
     *
     * @return View The newly instantiated view, or null.
     */
    private View createView(Context context, String name, AttributeSet attrs) {
        View view = null;
        try {
            if (-1 == name.indexOf('.')){
                if ("View".equals(name)) {
                    view = LayoutInflater.from(context).createView(name, "android.view.", attrs);
                }
                if (view == null) {
                    view = LayoutInflater.from(context).createView(name, "android.widget.", attrs);
                }
                if (view == null) {
                    view = LayoutInflater.from(context).createView(name, "android.webkit.", attrs);
                }
            }else {
                view = LayoutInflater.from(context).createView(name, null, attrs);
            }
        } catch (Exception e) {
            view = null;
        }
        return view;
    }

    /**
     * 更新配色
     */
    public void updateTheme(HashMap<Integer, Integer> themeConfig){
        if (themeConfig == null) {
            return;
        }
        for(SkinItem skinItem : mSkinItems){
            skinItem.apply(themeConfig);
        }
        //恢复完后直接清掉
        mSkinItems.clear();
    }
}
